
import os
from datetime import datetime
import xlrd
import pandas as pd
import numpy as np




def formatdate(date):
    return datetime.strptime(date, "%Y-%m-%d")

def matchDate(date1,date2):
    if date1 == date2:
        return True
    elif date1 != date2:
        return False

def dailyPlot():
    # df = pd.DataFrame({
    #     "日期":[],
    #     "日业绩":[],
    #     "泰森":[],
    #     "圣农":[],
    #     "日访客":[],
    #     "日转化":[],
    #     "日买家":[],
    #     "日客单":[]
    # })
    data = {
        "日期":[],
        "日业绩":[],
        "泰森业绩":[],
        "圣农业绩":[],
        "日访客":[],
        "日转化":[],
        "日买家":[],
        "日客单":[]
    }
    tempdate = []
    tempyeji = []
    tempzhuanhua = []
    tempmaijia = []
    tempkedan = []
    tempfangke = []
    temptaisen = []
    tempshengnong = []
    for root, dirs, files in os.walk("./in"):
        # print(root) #当前目录路径 
        # print(dirs) #当前路径下所有子目录 
        # print(files) #当前路径下所有非目录子文件
        for file in files:
            if "宏观监控-核心指标监控" in os.path.splitext(file)[0]:
                bookhongguan = xlrd.open_workbook(root + "/" + file)
                ## bookhongguan = load_workbook(root + "/" + file)
                bookhongguansheet = bookhongguan.sheets()[0]
                row =bookhongguansheet.nrows#总行数
                importhongguan  = []
                for index,i in enumerate(range(row)):
                    if index>5:
                        temp = bookhongguansheet.row_values(i)
                        # temp[0] = formatdate(temp[0])
                        importhongguan.append(temp)
                for index,i in enumerate(importhongguan):
                    tempdate.append(formatdate(i[0]))
                    tempyeji.append(float(i[2].replace(",","")))
                    tempzhuanhua.append(float(i[21].replace("%","")))
                    tempmaijia.append(float(i[13]))
                    tempkedan.append(float(i[26]))
            
            elif "竞店分析 全店 所有终端" in os.path.splitext(file)[0]:
                if os.path.splitext(file)[1] == ".xlsx":
                    bookjingpin = xlrd.open_workbook(root + "/" + file)
                    ## bookjingpin = load_workbook(root + "/" + file)
                    bookjingpinsheet = bookjingpin.sheets()[0]
                    row =bookjingpinsheet.nrows#总行数
                    importjingpin  = []
                    for index,i in enumerate(range(row)):
                        if index>0:
                            temp = bookjingpinsheet.row_values(i)
                            # temp[0] = formatdate(temp[0])
                            importjingpin.append(temp)
                    # print(importjingpin)

                    for index,i in enumerate(tempdate):
                        tempfangke.append("")
                        temptaisen.append("")
                        tempshengnong.append("")
                        for index1,i1 in enumerate(importjingpin):
                            if matchDate(i,i1[2]):
                                if i1[0] == "太阳谷旗舰店":
                                    tempfangke[index] = float(i1[4])#日访客
                                elif i1[0] == "Tyson泰森食品官方旗舰店":
                                    temptaisen[index] = float(i1[3])#店1
                                elif i1[0] == "圣农旗舰店":
                                    tempshengnong[index] = float(i1[3])#店2


        data["日期"] = tempdate
        data["日业绩"] = tempyeji
        data["日转化"] = tempzhuanhua
        data["日买家"] = tempmaijia
        data["日客单"] = tempkedan
        data["日访客"] = tempfangke
        data["泰森业绩"] = temptaisen
        data["圣农业绩"] = tempshengnong
        df = pd.DataFrame(data).set_index("日期")
        ax=df.plot(title='日报',subplots=True)#,y=["日业绩","日转化","日买家","日客单","日访客","泰森业绩","圣农业绩"],
        for index,item in enumerate(ax):
            fig=item.get_figure()
            name = "日报.png"
            fig.savefig(name)